package com.upcode.weighing.util;


/* * 
 *    敏感信息屏蔽工具
 */
public final class SensitiveInfoUtils {
	
	/**
	 * 身份证号除了前4位和后5位中间脱敏
	 * @param str
	 * @return
	 */
	public static String sensitiveIdCard(String str) {
		return sensitiveString(str,4,5,'*');
	}
	
	/**
	 * 手机号除了前3位和后4位中间脱敏
	 * @param str
	 * @return
	 */
	public static String sensitiveTel(String str) {
		return sensitiveString(str,3,4,'*');
	}
	
	/**
	 * 银行卡号除了前4位和后4位中间脱敏
	 * @param str
	 * @return
	 */
	public static String sensitiveBankCard(String str) {
		return sensitiveString(str,4,4,'*');
	}

	/**
	 * 业主姓名
	 * 如果姓名小于4位则后3位脱敏
	 * 如果姓名大于等于5位则全部脱敏
	 * */
	public static String sensitiveName(String str){
		if (str.length() >= 5){
			return sensitiveString(str,0,0,'*');
		}else {
			return sensitiveString(str,1,0,'*');
		}
	}
	
	
	private static String sensitiveString(String str,int leftLen,int rightLen,char mask) {
		if(str == null) {
			return str;
		}
		
		if(str.isEmpty()) {
			return str;
		}
		
		if(leftLen < 0 || rightLen < 0) {
			throw new RuntimeException("非法参数");
		}
		
		if(str.length() <= leftLen+rightLen) {
			return str;
		}
		
		StringBuilder strBuilder = new StringBuilder(str);
		int end = str.length() - rightLen;
		for(int i = leftLen;i < end;i++) {
			strBuilder.setCharAt(i, mask);
		}
		
		return strBuilder.toString();
	}
	
 
}
